package pwd.allen.androiddemo.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import pwd.allen.androiddemo.R;
import pwd.allen.androiddemo.util.ToastUtil;

/**
 * 补间动画，执行完后会恢复最初的状态
 * 属性动画
 * @see android.animation.ValueAnimator
 */
public class AnimationActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    private static final String[] selects = {"alpha", "rotate", "scale", "translate", "valueAnimator", "objectAnimator"};

    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animation);
        textView = findViewById(R.id.tv);

        Spinner sp = findViewById(R.id.spinner);
        sp.setAdapter(new ArrayAdapter<String>(this, R.layout.item_select, selects));
        sp.setOnItemSelectedListener(this);

        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f, 1f);
        valueAnimator.setDuration(2000);
        valueAnimator.addUpdateListener(animator -> {
            float value = (float) animator.getAnimatedValue();
            Log.i("mennalisha", "onAnimationUpdate:" + value);
        });
        valueAnimator.start();
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        Animation animation = null;
        switch (selects[position]) {
            case "alpha":
                animation = AnimationUtils.loadAnimation(this, R.anim.alpha);
                textView.startAnimation(animation);
                break;
            case "rotate":
                animation = AnimationUtils.loadAnimation(this, R.anim.rotate);
                textView.startAnimation(animation);
                break;
            case "scale":
                animation = AnimationUtils.loadAnimation(this, R.anim.scale);
                textView.startAnimation(animation);
                break;
            case "translate":
                animation = AnimationUtils.loadAnimation(this, R.anim.translate);
                textView.startAnimation(animation);
                break;
            case "valueAnimator":
                // 属性动画，从0变为1
                ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f, 1f);
                valueAnimator.setDuration(1000);
                valueAnimator.addUpdateListener(animator -> {
                    textView.setAlpha((Float) animator.getAnimatedValue());
                });
                valueAnimator.start();
                break;
            case "objectAnimator":
                // 属性动画，设置textView的透明度由0到1
                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView, "alpha", 0f, 1f);
                objectAnimator.setDuration(1000);
                objectAnimator.addListener(new AnimatorListenerAdapter() {
                    @Override
                    public void onAnimationStart(Animator animation) {
                        super.onAnimationStart(animation);
                        ToastUtil.show(AnimationActivity.this, "属性动画开始");
                    }
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        ToastUtil.show(AnimationActivity.this, "属性动画结束");
                    }
                });
                objectAnimator.start();
                break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
    }
}
